home *** CD-ROM | disk | FTP | other *** search
- Path: castle.nando.net!news
- From: actuary@nando.net (Bill McCarthy)
- Newsgroups: comp.lang.c
- Subject: Re: Can't figure this out
- Date: 22 Jan 1996 03:54:41 GMT
- Organization: News & Observer Public Access
- Message-ID: <4dv1q1$pbl@castle.nando.net>
- References: <31000091.3778302@news.panix.com>
- Reply-To: actuary@nando.net (Bill McCarthy)
- NNTP-Posting-Host: vyger119.nando.net
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <31000091.3778302@news.panix.com>, dm@panix.com (Dan'l) writes:
- >I am learning C and I have not had any problems understanding most
- >concepts I have learned so far. But to date I still can't figure out
- >how the outcome of this program is 15. Somehow one of the B's ends up
- >a three and the other B a 5, or am I so off base that I can't see
- >what's really happening. Can someone please walk me through this
- >one. Thanks Dan'l
- >
- >#define A 3
- >#define B A + A
- >#define C B * B
- >
- >main()
- >{
- > printf("%d", C);
- > return 0;
- >}
-
- The preprocessor is doing text substitution. So B will be replaced by
- 3 + 3. And C will be replaced by 3 + 3 * 3 + 3. That evaluates to
- 3 + 9 + 3 which evaluates to 15.
-
- The solution is to use parenthesis (a reductant set of parenthesis
- may cost a few microseconds of compile time, but there's no cost at
- execution time). Try:
-
- #define B ((A) + (A))
- #define C ((B) * (B))
-
- Bill McCarthy
- actuary@nando.net
- Wendell, NC USA
-
-